<?php

require_once("MDB2.php");
require_once("config.php");

function checkLogin($arr) {
	$user=$arr['PHP_AUTH_USER'];
	$pass=$arr['PHP_AUTH_PW'];
	if( ($user == __USER__) && ($pass == __PASS__) ) {
		return true;
	} else {
		return false;
	}
}
function connectDB() {
	$db=MDB2::connect(__DSN__);
	if(PEAR::isError($db)) {
		print $db->getMessage();
		print "<br>";
		print "<pre>";
		var_dump($db);
		print "</pre>";
		exit();
	} else {
		return $db;
	}
}

function addLines($id) {
	$rv="<form method=\"get\" action=\"addscore.php\"><input type=\"hidden\" name=\"bowler\" value=\"{$id}\"><tr>";
	$rv.="<td>Handicap</td><td colspan=\"2\"><input type=\"text\" size=\"3\" name=\"handicap\"></td>";
	$rv.="<td>Score</td><td colspan=\"2\"><input type=\"text\" name=\"pins\" size=\"3\">";
	$rv.="<td colspan=\"2\">&nbsp;</td>";
	$rv.="</tr>";
	$rv.="<tr>";
	$rv.="<td>Date</td><td colspan=\"5\"><input type=\"text\" name=\"date\" size=\"10\"></td>";
	$rv.="<td colspan=\"2\" rowspan=\"1\" align=\"center\" valign=\"middle\"><input type=\"submit\" value=\"Post\"></td>";
	$rv.="</tr>";
	return $rv;
}


function update($db,$bowler) {
	$sql="UPDATE bowlers SET games=(SELECT count(bowler_id) FROM games WHERE bowler_id={$bowler}) WHERE id={$bowler}";
	$result=$db->query($sql);
	dieError($result,$sql);
	$sql="UPDATE bowlers SET avg=(SELECT avg(pins) FROM games WHERE bowler_id={$bowler}) WHERE id={$bowler}";
	$result=$db->query($sql);
	dieError($result,$sql);
	$sql="UPDATE bowlers SET pins=(SELECT sum(pins) FROM games WHERE bowler_id={$bowler}) WHERE id={$bowler}";
	$result=$db->query($sql);
	dieError($result,$sql);
	$sql="UPDATE bowlers SET score=(SELECT sum(total) FROM games WHERE bowler_id={$bowler}) WHERE id={$bowler}";
	$result=$db->query($sql);
	dieError($result,$sql);
	$sql="SELECT avg FROM bowlers WHERE id={$bowler}";
	$result=$db->query($sql);
	dieError($result,$sql);
	$row=$result->fetchRow($sql);
	$avg=$row[0];
	$avg=round($avg);
	if( ($avg < 210) && ($avg != 0) ) {
		$hcp=round((210-$avg)*.9);
	} else {
		$hcp=0;
	}
	$sql="UPDATE bowlers SET avg={$avg}, hcp={$hcp} WHERE id={$bowler}";
	$result=$db->query($sql);
	dieError($result,$sql);
}
function dieError($item,$sql) {
	if(PEAR::isError($item)) {
		print $sql."<br>";
		print $item->getMessage();
		exit();
	}
}
function initDB() {
	$db = MDB2::connect(__DSN__);
	if(PEAR::isError($db)) {
		print $db->getMessage();
		exit();
	}
	$sql[]="DROP TABLE bowlers";
	$sql[]="DROP TABLE games";
	$sql[]="CREATE TABLE bowlers (id INTEGER AUTOINCREMENT PRIMARY KEY,name varchar,games int default 0,avg int default 0,hcp int default 0,pins int default 0, score int default 0)";
	$sql[]="CREATE TABLE games (id INTEGER AUTOINCREMENT PRIMARY KEY,date date,bowler_id int,pins int, handicap int, total int)";
	//$sql[]="ALTER TABLE bowlers ADD constraint bowlers_pkey PRIMARY KEY (id)";
	//$sql[]="ALTER TABLE games ADD constraint games_pkey PRIMARY KEY (id)";
	//$sql[]="ALTER TABLE games ADD FOREIGN KEY (bowler_id) REFERENCES bowlers (id)";
	foreach($sql as $s) {
		$res=$db->query($s);
		if(PEAR::isError($res)) {
			print $s;
			print "<br>\n";
			print $res->getMessage();
			print "<br>\n";
		} else {
			print "OK -- ";
			print $s;
			print "<br>\n";
		}
	}
	$db->disconnect();
	chmod(__DBFILE__,0666);
	print "<a href=\"index.php\">Index</a><br>";
}
function getPlayerList() {
}

function getAverage($player_id) {
}

function getHandicap($player_id) {
}
